package com.talkweb.securitycamera.utils.eventlib.communicationevent.dispatch;

import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEvent;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEventManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * @version 1.0.1
 * @description:主线程分发器
 * @program: Gallery
 * @Author xiaozhijun
 * @Date 2020/12/24 9:55
 */
public class MainDispatch implements IDispatch {

    EventHandler eventHandler;

    @Override
    public synchronized void post(PhotoEvent event) {
        if (!isMainThread()) {
            if (eventHandler == null) {
                eventHandler = new EventHandler(EventRunner.getMainEventRunner());
            }
            eventHandler.postTask(() -> {
                PhotoEventManager.getInstance().post(event);
            });
        }
    }


    public boolean isMainThread() {
        return EventRunner.current() == EventRunner.getMainEventRunner();
    }
}
